<?php
class Login extends CI_Controller{

	function go(){
		$this->load->library('ci_smarty');
		$this->ci_smarty->display('admin/login.html');
	}

	//验证登录
	function is_login(){
		$this->load->model('server/news_model');

		$username = $_POST["username"];
		$password = $_POST["password"];

		$password_hash=hash("sha256",$password);

		//用户名与密码查询
		$cont=[
			'table'=>'admin',
			'where'=>[
				'username'=>$username,
				'password'=>$password_hash
			]
		];

		$data = $this->news_model->select_form($cont);  //返回数据的数组
		if ($data==null){
			$content = [
				'code' => 400,
				'data' => '登录失败！'
			];
		}else{
			$content = [
				'code' => 200,
				'data' => '登录成功！'
			];
			//写入缓存
			$this->load->library('session');
			$this->session->set_userdata('username', 'isok');
		}
		echo json_encode($content, JSON_UNESCAPED_UNICODE) . PHP_EOL;
	}

	public function ex_login(){
		//写入缓存
		$this->load->library('session');
		$this->session->unset_userdata('username');
		$content = [
			'code' => 200,
			'data' => '退出成功！'
		];
		echo json_encode($content, JSON_UNESCAPED_UNICODE) . PHP_EOL;
	}
}
